package com.example.food.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.food.adapter.FoodProductsAdapter;
import com.example.food.bean.OrderListDetailsBean;
import com.example.home.R;
import com.example.home.tool.OKHttpTool;

import java.util.List;

public class OrderActivity extends AppCompatActivity {

    private String orderNoNum;
    private ImageView img;
    private TextView paymentType;
    private TextView orderNo;
    private TextView payTime;
    private TextView receiverPhone;
    private TextView receiverAddress;
    private RecyclerView refoodlist;
    private TextView name;
    private TextView monrytotle;
    private TextView status;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);

        initView();
        getData();
    }

    private void getData() {
        String url= "prod-api/api/takeout/order/"+orderNoNum;
        OKHttpTool.doNetRequest(url,
                OKHttpTool.GET,
                OrderListDetailsBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        OrderListDetailsBean orderListDetailsBean = (OrderListDetailsBean) obj;
                       runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if(orderListDetailsBean.getCode()==200){
                                    OrderListDetailsBean.DataBean data = orderListDetailsBean.getData();
                                    OrderListDetailsBean.DataBean.OrderInfoBean orderInfo = data.getOrderInfo();
                                    OrderListDetailsBean.DataBean.SellerInfoBean sellerInfo = data.getSellerInfo();

                                    name.setText(sellerInfo.getName());
                                    receiverAddress.setText(orderInfo.getReceiverAddress());
                                    receiverPhone.setText(orderInfo.getReceiverPhone());
                                    payTime.setText(orderInfo.getPayTime());
                                    orderNo.setText(orderInfo.getOrderNo());
                                    paymentType.setText(orderInfo.getPaymentType());
                                    status.setText(orderInfo.getStatus());
                                    monrytotle.setText("合计: "+String.valueOf(orderInfo.getAmount()));

                                    List<OrderListDetailsBean.DataBean.OrderInfoBean.OrderItemListBean> orderItemList = orderInfo.getOrderItemList();
                                    refoodlist.setLayoutManager(new LinearLayoutManager(OrderActivity.this));
                                    refoodlist.setAdapter(new FoodProductsAdapter(OrderActivity.this, orderItemList,"OrderdetailsActivity"));
                                }
                            }
                        });
                    }
                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void initView() {

        Intent intent = getIntent();
        orderNoNum = intent.getStringExtra("orderNo");

        img = findViewById(R.id.img);
        name = findViewById(R.id.name);
        refoodlist = findViewById(R.id.refoodlist);
        receiverAddress = findViewById(R.id.receiverAddress);
        receiverPhone = findViewById(R.id.receiverPhone);
        payTime = findViewById(R.id.payTime);
        orderNo = findViewById(R.id.orderNo);
        paymentType = findViewById(R.id.paymentType);
        monrytotle = findViewById(R.id.monrytotle);
        status = findViewById(R.id.status);

        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OrderActivity.this.finish();
            }
        });
    }

}
